package com.etbsms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etbsms.dao.IFilesDAO;
import com.etbsms.domain.CodeCountry;
import com.etbsms.domain.CodeLanguage;
import com.etbsms.domain.CodePlant;
import com.etbsms.domain.LogLogin;
import com.etbsms.domain.RecNumber;
import com.etbsms.domain.ReportProblem;
import com.etbsms.domain.Role;
import com.etbsms.domain.Users;
import com.etbsms.security.User;
import com.etbsms.service.ICodeCountryService;
import com.etbsms.service.ICodeLanguageService;
import com.etbsms.service.ICodePlantService;
import com.etbsms.service.ILogLoginService;
import com.etbsms.service.IRecNumberService;
import com.etbsms.service.IReportProblemService;
import com.etbsms.service.IRoleService;
import com.etbsms.service.IUsersService;
import com.etbsms.util.Constants;

@Controller
@RequestMapping(value = "/index/**")
@SuppressWarnings("unused")
public class IndexController {

	@Autowired
	private IRoleService roleService;

	@Autowired
	private IUsersService usersService;

	@Autowired
	private ICodeLanguageService codeLanguageService;

	@Autowired
	private IRecNumberService recNumberService;
	
	@Autowired
	private ICodePlantService codePlantService;
	
	@Autowired
	private ICodeCountryService codeCountryService;

	@Autowired
	private IReportProblemService reportProblemService;

	@Autowired
	private ILogLoginService logLoginService;
	
	@Autowired
	private IFilesDAO filesDAO;
	
	
	@RequestMapping(value = "/index/init.htm")
	public String home(HttpServletRequest req, ModelMap map) {
		
		roleService.init();
		usersService.init();
		
		CodeLanguage cLan = new CodeLanguage();
		cLan.setCode("TC");
		cLan.setName("中文繁體");
		cLan.setSort(1);
		codeLanguageService.save(cLan);
		
		cLan.setCode("SC");
		cLan.setName("中文簡體");
		cLan.setSort(2);
		codeLanguageService.save(cLan);
		
		cLan.setCode("EN");
		cLan.setName("英文");
		cLan.setSort(3);
		codeLanguageService.save(cLan);
		
		cLan.setCode("JP");
		cLan.setName("日文");
		cLan.setSort(4);
		codeLanguageService.save(cLan);
		
		RecNumber recNumer = new RecNumber();
		recNumer.setSeq_no(1);
		recNumer.setNumber(0);		
		recNumberService.save(recNumer);	
		
		
		CodePlant codePlant = new CodePlant();
		codePlant.setCode("A1");
		codePlant.setName_ch("農糧作物");		
		codePlantService.save(codePlant);
		
		codePlant.setCode("A2");
		codePlant.setName_ch("特用作物");
		codePlantService.save(codePlant);
		
		codePlant.setCode("A3");
		codePlant.setName_ch("園藝作物－食用");
		codePlantService.save(codePlant);
		
		codePlant.setCode("A4");
		codePlant.setName_ch("園藝作物－觀賞");
		codePlantService.save(codePlant);
		
		codePlant.setCode("A5");
		codePlant.setName_ch("其他");
		codePlantService.save(codePlant);
		
		
		CodeCountry codeCountry = new CodeCountry();
		codeCountry.setCode("A1");
		codeCountry.setName_ch("中南美地區");
		codeCountryService.save(codeCountry);
		
		codeCountry.setCode("A2");
		codeCountry.setName_ch("亞太地區");
		codeCountryService.save(codeCountry);
		
		codeCountry.setCode("A3");
		codeCountry.setName_ch("北美地區");
		codeCountryService.save(codeCountry);
		
		codeCountry.setCode("A4");
		codeCountry.setName_ch("歐洲地區");
		codeCountryService.save(codeCountry);
		
		codeCountry.setCode("A5");
		codeCountry.setName_ch("西亞地區");
		codeCountryService.save(codeCountry);
		
		codeCountry.setCode("A6");
		codeCountry.setName_ch("非洲地區");
		codeCountryService.save(codeCountry);
			
		return "index";
	}
	
	@RequestMapping(value = "/index/login.htm")
	public String login(@RequestParam(value="username")String username,@RequestParam(value="password")String password,ModelMap map,HttpServletRequest req) {		
		Users users = usersService.get(username, password);
		if(users == null){
			map.addAttribute("LOGIN_ERROR",Constants.LOGIN_ERROR_PASWOD);
			return Constants.PAGE_INDEX;
		}
		
		map.addAttribute("USERS",users);
		if(users.getRole().size() == 1){
			Role role = new Role();
			for(Role _r : users.getRole()){
				role = _r;
			}
			//Constants.CURRENT_ROLE = role;			
			Constants.CURRENT_ROLE_MAP.put(users.getSeq_no(), role);
			LogLogin logEntity = new LogLogin();
			logEntity.setRemote_ip(req.getRemoteAddr());
			logEntity.setUsers(users);
			logEntity.setRole(role);
			logLoginService.save(logEntity);				
			return "index_3";
		}
		map.addAttribute("ROLES",users.getRole());	
		return Constants.INDEX_CHOSEROLE;
	}
	
	@RequestMapping(value = "/index/login2.htm")
	public String login(@RequestParam(value="username")String username,@RequestParam(value="password")String password,@RequestParam(value="role")String role,ModelMap map,HttpServletRequest req) {
		//Constants.CURRENT_ROLE = roleService.get(role);
		Users users = usersService.get(username, password);	
		Role currentRole = roleService.get(role);
		Constants.CURRENT_ROLE_MAP.put(users.getSeq_no(), currentRole);
		LogLogin logEntity = new LogLogin();
		logEntity.setRemote_ip(req.getRemoteAddr());
		logEntity.setUsers(users);
		logEntity.setRole(currentRole);
		logLoginService.save(logEntity);
		map.addAttribute("USERS",users);
		return "index_3";
	}
	
	@RequestMapping(value = "/index/test.htm")
	public String test() {
		//Constants.CURRENT_ROLE = roleService.get(role);
		filesDAO.info();
		return "index";
	}
	
	
	
	
	
	
	public ModelMap setMainMenu(ModelMap map){
		map.addAttribute("MAINMENU", Constants.MAIN_MENU_ADMIN);
		return map;
	}
	
	@RequestMapping(value = "/index/role/login.htm")
	public String roleLogin(ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();	
		return "redirect";
	}
	
	
	
	@RequestMapping(value = "/index/role/get.htm")
	@ResponseBody
	public List<Role> getRoles() {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();
		
		
		return roleService.getAll();
	}
	
	
	@RequestMapping(value = "/index/user/modify.htm")
	public String modify(HttpServletRequest req, ModelMap map) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();		
		return Constants.PAGE_USERMDY;
	}
	
	@RequestMapping(value = "/index/report.htm")
	@ResponseBody
	public boolean problemReport(ReportProblem form) {
		Users users = ((User)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsers();		
		form.setUsers(users);		
		return reportProblemService.save(form);
	}
	
}
